package com.soso.base.util.log;

import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;


public class JDK14Logger extends BaseLogger{

	public static final JDK14Logger DEFAULT;
	static{
		DEFAULT = getLoggerInstance("Default");
	}
	
	/** The JDK1.4 Logger being used */
    private final Logger logger;

    public static JDK14Logger getLoggerInstance(String logName){
    	return new JDK14Logger(logName);
    }
    /**
     * Constructor for a JDK 1.4 Logger.
     * @param logName Name of the logger
     */
    public JDK14Logger(String logName)
    {
        // Logging assumed to be configured by user via "java.util.logging.config.file"
        logger = Logger.getLogger(logName);
    }
    /**
     * Log a debug message.
     * @param msg The message
     */
    public void debug(String msg)
    {
        log(Level.FINE, msg, null);
    }

    /**
     * Log a debug message with throwable.
     * @param msg The message
     * @param thr A throwable
     */
    public void debug(String msg, Throwable thr)
    {
        log(Level.FINE, msg, thr);
    }

    /**
     * Log an info message.
     * @param msg The message
     */
    public void info(String msg)
    {
        log(Level.INFO, msg, null);
    }

    /**
     * Log an info message with throwable.
     * @param msg The message
     * @param thr A throwable
     */
    public void info(String msg, Throwable thr)
    {
        log(Level.INFO, msg, thr);
    }

    /**
     * Log a warning message.
     * @param msg The message
     */
    public void warn(String msg)
    {
        log(Level.WARNING, msg, null);
    }

    /**
     * Log a warning message with throwable.
     * @param msg The message
     * @param thr A throwable
     */
    public void warn(String msg, Throwable thr)
    {
        log(Level.WARNING, msg, thr);
    }

    /**
     * Log an error message.
     * @param msg The message
     */
    public void error(String msg)
    {
        log(Level.SEVERE, msg, null);
    }

    /**
     * Log an error message with throwable.
     * @param msg The message
     * @param thr A throwable
     */
    public void error(String msg, Throwable thr)
    {
        log(Level.SEVERE, msg, thr);
    }

    /**
     * Log a fatal message.
     * @param msg The message
     */
    public void fatal(String msg)
    {
        log(Level.SEVERE, msg, null);
    }

    /**
     * Log a fatal message with throwable.
     * @param msg The message
     * @param thr A throwable
     */
    public void fatal(String msg, Throwable thr)
    {
        log(Level.SEVERE, msg, thr);
    }

    /**
     * Accessor for whether debug logging is enabled
     * @return Whether it is enabled
     */
    public boolean isDebugEnabled()
    {
        return logger.isLoggable(java.util.logging.Level.FINE);
    }

    /**
     * Accessor for whether info logging is enabled
     * @return Whether it is enabled
     */
    public boolean isInfoEnabled()
    {
        return logger.isLoggable(java.util.logging.Level.INFO);
    }

    private void log(Level level, String msg, Throwable thrown)
    {
        if (msg == null) 
        {
            level = Level.SEVERE;
            msg = "Missing [msg] parameter";
        }

        if (logger.isLoggable(level))
        {
            LogRecord result = new LogRecord(level, msg);
            if (thrown != null)
                result.setThrown(thrown);
            StackTraceElement[] stacktrace = new Throwable().getStackTrace();
            for (int i = 0; i < stacktrace.length; i++)
            {
                StackTraceElement element = stacktrace[i];
                if (!element.getClassName().equals(JDK14Logger.class.getName()))
                {
                    result.setSourceClassName(element.getClassName());
                    result.setSourceMethodName(element.getMethodName());
                    break;
                }
            }
            logger.log(result);
        }
    }

}
